package com.zrrd.learn.chapter03;

/**
 * 快乐数
 *
 * @author wrn
 * @version 1.0
 * @date 2023/5/13 15:48
 */

public class HappyNum {

    int a = 19;

    public static boolean isHappy(int a) {


        int n = 0;
        while (a != 1){
            int len = count(a);
            int temp = 0;
            for(int i=0 ; i<len ; i++){
                int a1 = a/(int)(Math.pow(10,i))%10;
                temp += a1 * a1;
            }
            a=temp;

            if(n++ >= 100){
                return false;
            }
        }
        return  true;
    }

    public static int count(int n) {
        int index = 0;
        while (n != 0){
            n/=10;
            index++;
        }
        return index;

    }

    public static void main(String[] args) {
        System.out.println(isHappy(6789));
    }

}
